var this_id = '';

var PAGES = {
	
	//  初始化准备
	initial:function(){
		layui.use(function(){
		  var $ = layui.$;
		  var layer = layui.layer;
		  var util = layui.util;
		  var form = layui.form;
		  
		  // 事件 处理 新增和纠错功能的
		  util.on('lay-on', {
			    'tips-photos-one': function(e){
					var url = e.attr('src')
					layer.photos({
						photos: {
						  "title": "大图查看",
						  "start": 0,
						  "data": [{"pid": 5,"src": url,}]
						},
						footer: false
					});
			    },
			   'test-page-custom': function(e){
					var  this_id   = e.attr('data-id');
					var  this_type = e.attr('data-type');
					
					layer.open({
					  type: 1,
					  scrollbar:false,
					  area: '600px',
					  resize: false,
					  shadeClose: true,
					  title: [this_type == 0 ? '内容新增填写' : '内容纠错填写', 'font-size:16px;font-weight: 600'],
					  content: `
						<div class="layui-form" lay-filter="filter-test-layer" style="margin: 20px 16px 16px 16px;">
						  <div class="demo-login-container">
						  
							<div class="layui-form-item">
							  <div class="layui-input-wrap">
								<textarea name="content" value="" style="min-height: 165px;" lay-verify="required" placeholder="请填写内容" lay-reqtext="请填写内容" autocomplete="off" class="layui-textarea" lay-affix="clear"></textarea>
							  </div>
							</div>
							
							<div class="layui-form-item" style="margin-top:20px;display: flex;align-items: center;justify-content: flex-end;">
								<button style="width:100px" class="layui-btn" lay-submit lay-filter="demo-login">提交</button>
							</div>
						   
						  </div>
						</div>
					  `,
					  success: function(){
						// 对弹层中的表单进行初始化渲染
						form.render();
						
						// 表单提交事件
						form.on('submit(demo-login)', function(data){
						  var field = data.field; // 获取表单字段值
						  field.content_id = this_id;
						  field.type = this_type;
						  field.url  = window.location.href;
						  PAGES.setPageCorrect(field)
						  return false; // 阻止默认 form 跳转
						});
					}
				});
			}
		  				  
		  })
		  
		  
		});
		
		var jingjiang_storages_data = localStorage.getItem("jingjiang_storages_data");
		
		if(wxx.getParam()){
			localStorage.setItem("jingjiang_component_index", wxx.getParam().id);
		}
		
		this_id = wxx.getParam() ? wxx.getParam().id : localStorage.getItem("jingjiang_component_index");
		this_id && PAGES.get_brand_category(this_id)
		this_id && wxx.getPageNewLocal({page_size:4,brand_category_id:this_id,page:1})
		this_id && wxx.getPageExhibition({page_size:6,brand_category_id:this_id,page:1})
		this_id && wxx.getPageHot({page_size:5,brand_category_id:this_id,page:1,region_id:''})
		this_id && wxx.getPageArticle({brand_category_id:this_id,region_id:""})
		
		
		if(jingjiang_storages_data){
			try{
				var d = JSON.parse(jingjiang_storages_data)
				for (var i = 0; i < d.length; i++) {
					if(d[i].id == this_id){
						$('.page_type_title').html(d[i].name)
						document.title = d[i].name;
						break;
					}
				}
			}catch(e){
				console.log('通过ID获取 对应分类信息出错了！！！')
			}
		}
		
		
		$('.customer_service').click(function(){
			window.location.href="../../editor/releaseEditor.html?id="+this_id
		})
		
	},
	
	
	//  获取主页 排行榜数据
	getRanking:function(keyset){
		var datas = {
		 	url  :'/api/ranking', 
		 	http_type:'POST',
			datas:{
				relate_id:this_id,
				keyset:keyset
			}
		}
		wxx.HttpReqeust(datas,function(ret){
		 	if(ret.code == 1){
				var hrml = template('ranking_script', ret);
				$("#ranking_datas").append(hrml);
				PAGES.swiper_ranking();
		 	}
		});
	},
	
	//  获取 页面数据 例如 产业定义，产业概况 等 
	getPageDatas:function(keyset){
		var datas = {
		 	url  :'/api/page', 
		 	http_type:'POST',
			datas:{
				relate_id:this_id,
				keyset:keyset
			}
		}
		wxx.HttpReqeust(datas,function(ret){
		 	if(ret.code == 1){
				var hrml = template('script_page', ret.data);
				$("#view_page").empty().append(hrml);
				
				//  调用一下公共分类展开收起等方法
				wxx.init_types(true);
				
				$('#view_page img').each(function(){
					var url = $(this).attr('src');
					$(this).addClass('fancybox').attr('data-fancybox-group','gallery').attr('href',url).attr('title','点击查看大图').attr('lay-on','tips-photos-one')
				})
				
				
		 	}
		});
	}, 
	
	
	
	//  获取分类数据
	get_brand_category:function(id){
		var datas = {
		 	url  :'/api/brand_category/getData', 
		 	http_type:'POST',
			datas:{
				parent_id:id
			}
		}
		wxx.HttpReqeust(datas,function(ret){
		 	if(ret.code == 1){
				//  把所属分类id 记录到分类列表里面 下级分类查询分类也用这个id；
				if(this_id){
					for (var i = 0; i < ret.data.length; i++) {
						ret.data[i].max_id = this_id*1
					}
				}
				var hrml = template('SHOW_TYPE_SCRIPT', ret);
				$("#SHOW_TYPE").empty().append(hrml); 
				
				if(ret.data && ret.data.length){
					//  调用一下公共分类展开收起等方法
					wxx.init_types(true);
					$('.m_alls').show();
					
					//  查询一下是否每个分类都是有子级  全部没有的话  隐藏【全部展开】
					var is = false;
					for (var i = 0; i < ret.data.length; i++) {
						if( ret.data[i].children && ret.data[i].children.length){
							is = true;
						}
					}
					if(is==false){$('.m_alls').hide();}
				}else{
					$('.m_alls').hide();
				}
		 	}
		});
	},
	
	
	// 提交 新增和纠错
	setPageCorrect:function(obj){
		var datas = {
		 	url  :'/api/page/correct', 
		 	http_type:'POST',
			loading:true,
			datas:obj
		}
		wxx.HttpReqeust(datas,function(ret){
			layer.alert(ret.msg,{
				title:'温馨提示',
			},function(){
				console.log(ret)
				if(ret.code == 1){
					layer.closeAll()
				}else{
					layer.closeLast(); 
				}
			});
		 	
		});
	},
	
	
	//  排名相关
	swiper_ranking:function(){
		if($('.Ranking').length){
			var w = $('.page_content_box').innerWidth();
			$('.Ranking').css('width',w)
			if(w){
				// 初始化swiper
				var swiper01 = new Swiper('.swiper_ranking', {
				  slidesPerView: 2,
				  spaceBetween: 20,
				  slidesPerGroup: 2,
				  loop: false,
				  loopFillGroupWithBlank: true,
				  pagination: {
					el: '.swiper-pagination',
					clickable: true,
				  },
				  navigation: {
					nextEl: '.swiper-button-next1',
					prevEl: '.swiper-button-prev1',
				  },
				});
			}
		}
	},
	
}


PAGES.initial();